<script setup lang="ts">
import { reactive, onMounted, ref } from "vue";
import { useRouter } from "vue-router";
import newUser from "@/assets/svg/new_users.svg?component";
import * as echarts from "echarts";
import { ElMessage } from "element-plus";
import type { ECharts } from "echarts";

const router = useRouter();

const welcomeData = reactive({
  userName: "管理员",
  greetText: getGreetText(),
  quickLinks: [
    { title: "商品管理", icon: "ShoppingBag", path: "/goods" },
    { title: "订单管理", icon: "List", path: "/orders" },
    { title: "用户管理", icon: "User", path: "/users" },
    { title: "数据统计", icon: "Chart", path: "/dashboard" }
  ],
  statistics: [
    {
      title: "今日订单",
      value: 126,
      unit: "单",
      trend: "+12%",
      color: "#409EFF"
    },
    {
      title: "商品总数",
      value: 1580,
      unit: "件",
      trend: "+5%",
      color: "#67C23A"
    },
    {
      title: "活跃用户",
      value: 2451,
      unit: "人",
      trend: "+18%",
      color: "#E6A23C"
    },
    {
      title: "总销售额",
      value: 89654,
      unit: "元",
      trend: "+25%",
      color: "#F56C6C"
    }
  ]
});

function getGreetText() {
  const hour = new Date().getHours();
  if (hour < 6) return "夜深了";
  if (hour < 11) return "早上好";
  if (hour < 14) return "中午好";
  if (hour < 18) return "下午好";
  return "晚上好";
}

onMounted(() => {
  initOrderChart();
  initUserChart();
});

function initOrderChart() {
  const chart = echarts.init(document.getElementById("orderChart"));
  chart.setOption({
    title: { text: "近7日订单趋势", left: "center" },
    tooltip: { trigger: "axis" },
    xAxis: {
      type: "category",
      data: ["周一", "周二", "周三", "周四", "周五", "周六", "周日"]
    },
    yAxis: { type: "value" },
    series: [
      {
        data: [150, 230, 224, 218, 135, 147, 260],
        type: "line",
        smooth: true
      }
    ]
  });
}

function initUserChart() {
  const chart = echarts.init(document.getElementById("userChart"));
  chart.setOption({
    title: { text: "用户分布", left: "center" },
    tooltip: { trigger: "item" },
    series: [
      {
        type: "pie",
        radius: "50%",
        data: [
          { value: 1048, name: "新用户" },
          { value: 735, name: "老用户" },
          { value: 580, name: "VIP用户" }
        ],
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: "rgba(0, 0, 0, 0.5)"
          }
        }
      }
    ]
  });
}

// 待办事项
const todoList = ref([
  {
    type: "订单",
    content: "新订单待确认 - 汉服写真套餐",
    time: "10分钟前",
    status: "待处理"
  },
  {
    type: "客服",
    content: "用户咨询待回复",
    time: "30分钟前",
    status: "紧急"
  },
  {
    type: "系统",
    content: "系统更新提醒",
    time: "2小时前",
    status: "普通"
  }
]);

// 初始化图表
let orderChart: ECharts | null = null;
let userChart: ECharts | null = null;

const initCharts = () => {
  // 订单趋势图表
  const orderChartDom = document.getElementById("orderChart");
  if (orderChartDom) {
    orderChart = echarts.init(orderChartDom);
    const orderOption = {
      title: {
        text: "订单趋势",
        left: "center"
      },
      tooltip: {
        trigger: "axis"
      },
      xAxis: {
        type: "category",
        data: ["周一", "周二", "周三", "周四", "周五", "周六", "周日"],
        axisLine: {
          lineStyle: {
            color: "#ddd"
          }
        }
      },
      yAxis: {
        type: "value",
        axisLine: {
          show: false
        },
        splitLine: {
          lineStyle: {
            color: "#eee"
          }
        }
      },
      series: [
        {
          name: "订单数",
          type: "line",
          smooth: true,
          data: [120, 132, 101, 134, 90, 230, 210],
          itemStyle: {
            color: "#409EFF"
          },
          areaStyle: {
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              {
                offset: 0,
                color: "rgba(64,158,255,0.3)"
              },
              {
                offset: 1,
                color: "rgba(64,158,255,0.1)"
              }
            ])
          }
        }
      ]
    };
    orderChart.setOption(orderOption);
  }

  // 用户分析图表
  const userChartDom = document.getElementById("userChart");
  if (userChartDom) {
    userChart = echarts.init(userChartDom);
    const userOption = {
      title: {
        text: "用户分析",
        left: "center"
      },
      tooltip: {
        trigger: "item"
      },
      legend: {
        orient: "vertical",
        left: "left",
        top: "middle"
      },
      series: [
        {
          name: "用户构成",
          type: "pie",
          radius: ["40%", "70%"],
          avoidLabelOverlap: false,
          itemStyle: {
            borderRadius: 10,
            borderColor: "#fff",
            borderWidth: 2
          },
          label: {
            show: false
          },
          emphasis: {
            label: {
              show: true,
              fontSize: 14,
              fontWeight: "bold"
            }
          },
          labelLine: {
            show: false
          },
          data: [
            { value: 1048, name: "普通用户" },
            { value: 735, name: "VIP用户" },
            { value: 580, name: "商家用户" },
            { value: 484, name: "游客" }
          ]
        }
      ]
    };
    userChart.setOption(userOption);
  }
};

// 处理待办事项
const handleTodo = (item: any) => {
  ElMessage.success(`正在处理: ${item.content}`);
};

// 添加日期和天气文本
const currentDate = ref(
  new Date().toLocaleDateString("zh-CN", {
    year: "numeric",
    month: "long",
    day: "numeric",
    weekday: "long"
  })
);
const weatherText = ref("晴朗");

// 添加图表时间范围
const chartTimeRange = ref("week");

// 添加图标映射函数
const getStatIcon = (title: string) => {
  const iconMap: Record<string, string> = {
    今日订单: "ShoppingCart",
    商品总数: "Goods",
    活跃用户: "User",
    总销售额: "Money"
  };
  return iconMap[title] || "DataLine";
};

// 添加标签类型判断函数
const getTagType = (status: string) => {
  switch (status) {
    case "紧急":
      return "danger";
    case "待处理":
      return "warning";
    default:
      return "info";
  }
};

defineOptions({
  name: "Welcome"
});
</script>

<template>
  <div class="welcome-container">
    <!-- 顶部欢迎区域 -->
    <div class="welcome-header">
      <div class="header-content">
        <div class="greeting-section">
          <div class="greeting-text">
            <h1>{{ welcomeData.greetText }}，{{ welcomeData.userName }}</h1>
            <p class="subtitle">今天是{{ currentDate }}，祝您工作愉快！</p>
          </div>
          <div class="quick-stats">
            <div v-for="stat in quickStats" :key="stat.label" class="stat-item">
              <span class="label">{{ stat.label }}</span>
              <span class="value">{{ stat.value }}</span>
            </div>
          </div>
        </div>
        <div class="decoration">
          <newUser class="welcome-svg" />
        </div>
      </div>
    </div>

    <!-- 数据概览卡片 -->
    <div class="data-overview">
      <el-row :gutter="20">
        <el-col
          v-for="card in welcomeData.statistics"
          :key="card.title"
          :span="6"
        >
          <div class="data-card" :class="card.type">
            <div class="card-content">
              <div class="icon-wrapper">
                <el-icon><component :is="getStatIcon(card.title)" /></el-icon>
              </div>
              <div class="data-info">
                <div class="main-value">
                  {{ card.value }}<span class="unit">{{ card.unit }}</span>
                </div>
                <div class="data-title">{{ card.title }}</div>
              </div>
            </div>
            <div class="trend-info">
              <span class="trend-value">{{ card.trend }}</span>
              <span class="trend-text">较上周</span>
            </div>
          </div>
        </el-col>
      </el-row>
    </div>

    <!-- 图表区域 -->
    <div class="chart-section">
      <el-row :gutter="20">
        <el-col :span="16">
          <div class="chart-card">
            <div class="chart-header">
              <h3>订单趋势</h3>
              <el-radio-group v-model="chartTimeRange" size="small">
                <el-radio-button label="week">本周</el-radio-button>
                <el-radio-button label="month">本月</el-radio-button>
              </el-radio-group>
            </div>
            <div id="orderChart" class="chart-container" />
          </div>
        </el-col>
        <el-col :span="8">
          <div class="chart-card">
            <div class="chart-header">
              <h3>用户分布</h3>
            </div>
            <div id="userChart" class="chart-container" />
          </div>
        </el-col>
      </el-row>
    </div>

    <!-- 待办事项 -->
    <div class="todo-section">
      <div class="todo-header">
        <h3>待办事项</h3>
        <el-button type="primary" text>查看全部</el-button>
      </div>
      <div class="todo-list">
        <div
          v-for="(item, index) in todoList"
          :key="index"
          class="todo-item"
          @click="handleTodo(item)"
        >
          <div class="todo-content">
            <el-tag
              :type="getTagType(item.status)"
              effect="plain"
              size="small"
              >{{ item.type }}</el-tag
            >
            <span class="todo-text">{{ item.content }}</span>
          </div>
          <div class="todo-meta">
            <span class="time">{{ item.time }}</span>
            <el-icon class="arrow"><ArrowRight /></el-icon>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.welcome-container {
  padding: 24px;
  background: #f5f7fa;

  .welcome-header {
    background: linear-gradient(135deg, #1890ff 0%, #36cfc9 100%);
    border-radius: 16px;
    padding: 40px;
    margin-bottom: 24px;
    color: white;
    box-shadow: 0 8px 24px rgba(24, 144, 255, 0.15);

    .header-content {
      display: flex;
      justify-content: space-between;
      align-items: center;

      .greeting-section {
        .greeting-text {
          h1 {
            font-size: 36px;
            font-weight: 600;
            margin-bottom: 12px;
            letter-spacing: 1px;
          }

          .subtitle {
            font-size: 16px;
            opacity: 0.9;
            margin-bottom: 24px;
          }
        }

        .quick-stats {
          display: flex;
          gap: 32px;

          .stat-item {
            .label {
              display: block;
              font-size: 14px;
              opacity: 0.8;
              margin-bottom: 4px;
            }

            .value {
              font-size: 24px;
              font-weight: 600;
            }
          }
        }
      }

      .decoration {
        width: 300px;
        height: 200px;

        .welcome-svg {
          width: 100%;
          height: 100%;
        }
      }
    }
  }

  .data-overview {
    margin-bottom: 24px;

    .data-card {
      background: white;
      border-radius: 12px;
      padding: 24px;
      height: 140px;
      transition: all 0.3s ease;
      position: relative;
      overflow: hidden;
      box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);

      &:hover {
        transform: translateY(-4px);
        box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
      }

      .card-content {
        display: flex;
        align-items: flex-start;
        gap: 16px;
        margin-bottom: 16px;

        .icon-wrapper {
          width: 48px;
          height: 48px;
          border-radius: 12px;
          display: flex;
          align-items: center;
          justify-content: center;
          font-size: 24px;
          background: rgba(24, 144, 255, 0.1);
          color: #1890ff;
        }

        .data-info {
          .main-value {
            font-size: 28px;
            font-weight: 600;
            color: #1f2329;
            line-height: 1.2;

            .unit {
              font-size: 14px;
              margin-left: 4px;
              color: #86909c;
            }
          }

          .data-title {
            font-size: 14px;
            color: #4e5969;
            margin-top: 4px;
          }
        }
      }

      .trend-info {
        display: flex;
        align-items: center;
        gap: 8px;
        font-size: 13px;

        .trend-value {
          color: #00b578;
          font-weight: 500;
        }

        .trend-text {
          color: #86909c;
        }
      }
    }
  }

  .chart-section {
    margin-bottom: 24px;

    .chart-card {
      background: white;
      border-radius: 12px;
      padding: 24px;
      box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);

      .chart-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 24px;

        h3 {
          font-size: 16px;
          font-weight: 600;
          color: #1f2329;
          margin: 0;
        }
      }

      .chart-container {
        height: 360px;
      }
    }
  }

  .todo-section {
    background: white;
    border-radius: 12px;
    padding: 24px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);

    .todo-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 20px;

      h3 {
        font-size: 16px;
        font-weight: 600;
        color: #1f2329;
        margin: 0;
      }
    }

    .todo-list {
      .todo-item {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 16px;
        border-radius: 8px;
        cursor: pointer;
        transition: all 0.3s ease;

        &:hover {
          background: #f5f7fa;

          .arrow {
            transform: translateX(4px);
          }
        }

        .todo-content {
          display: flex;
          align-items: center;
          gap: 12px;

          .todo-text {
            font-size: 14px;
            color: #4e5969;
          }
        }

        .todo-meta {
          display: flex;
          align-items: center;
          gap: 16px;

          .time {
            font-size: 13px;
            color: #86909c;
          }

          .arrow {
            color: #86909c;
            transition: transform 0.3s ease;
          }
        }
      }
    }
  }
}
</style>
